package com.telecom.at.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.telecom.at.db.dac.Dac;
import com.telecom.at.db.dac.Paging;
import com.telecom.at.db.model.AvgFlow;
import com.telecom.at.db.model.NumOfAccount;
import com.telecom.at.db.model.User;

@WebServlet("/front/ListAvgFlowOfCity")
public class ListAvgFlowOfCity extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr == null)
			pageNoStr = "0";
		int pageNo = Integer.parseInt(pageNoStr);

		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr == null)
			pageSizeStr = "10";
		int pageSize = Integer.parseInt(pageSizeStr);

		if (request.getParameter("lastPage") != null)
			pageNo = Integer.MAX_VALUE;
		if (request.getParameter("firstPage") != null)
			pageNo = 0;
		if (request.getParameter("previousPage") != null)
			pageNo--;
		if (request.getParameter("nextPage") != null)
			pageNo++;

		List<AvgFlow> flowList = null;
		List<NumOfAccount> numList = null;
		User curUser = (User) request.getSession().getAttribute("curUser");
		if (curUser.getId() == 0)
			flowList = Dac.getInstance().allAvgFlows();
		else {
			flowList = Dac.getInstance().getAvgFlowOfUser(curUser.getCity());
			numList = Dac.getInstance().numOfAccount();
		}

		Paging<AvgFlow> paging = new Paging<AvgFlow>(pageSize, pageNo, flowList);
		System.out.println(numList);
		//NumOfAccount paging2 = new NumOfAccount(pageSize, pageNo, numList);
		request.setAttribute("paging", paging);
		request.setAttribute("numList", numList);
		//request.setAttribute("paging", paging2);
		request.getRequestDispatcher("/WEB-INF/jsp/front/ListAvgFlowOfCity.jsp").forward(request, response);

	}
}
